package com.zhsource.tourist.controller;

import com.github.pagehelper.PageInfo;
import com.zhsource.article.domain.Article;
import com.zhsource.article.domain.FriendAddress;
import com.zhsource.article.query.ArticleQuery;
import com.zhsource.article.service.IArticleService;
import com.zhsource.article.service.IArticleTypeService;
import com.zhsource.article.service.IFriendAddressService;
import com.zhsource.article.service.IParamService;
import com.zhsource.exception.GlobalException;
import com.zhsource.tourist.dto.ArticleDto;
import com.zhsource.tourist.vo.ArchivesVo;
import com.zhsource.tourist.vo.ArticleTypeCountVo;
import com.zhsource.tourist.vo.ArticleVo;
import com.zhsource.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>@ClassName：TouristArticleController
 * <p>@Date 2023/10/28  12:33
 *
 * @Author zzl
 **/
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/tourist/article")
public class TouristArticleController {

    @Autowired
    private IArticleService articleService;

    @Autowired
    private IArticleTypeService articleTypeService;

    @Autowired
    private IFriendAddressService friendAddressService;

    @Autowired
    private IParamService paramService;

    @Autowired
    private RedisTemplate redisTemplate;

    // 分页+ 模糊查询
    @PostMapping("/page")
    public R<PageInfo<Article>> page(@RequestBody ArticleQuery articleQuery) {
        try {
            return R.ok("分页查询成功!", articleService.page(articleQuery));
        } catch (GlobalException e) {
            log.error("Article模块的分页查询发生异常,异常信息为:{}", e.getMessage());
            return R.fail(e.getMessage());
        } catch (Exception e) {
            log.error("Article模块的分页查询发生异常,异常信息为:{}", e.getMessage());
            return R.fail("系统异常,请重试");
        }
    }

    /**
     * 首页分类查询
     *
     * @return
     */
    @GetMapping("/articleTypeCountVo")
    public R<List<ArticleTypeCountVo>> ArticleType() {
        try {
            return R.ok("分页查询成功!", articleTypeService.getArticleTypeCount());
        } catch (GlobalException e) {
            log.error("Article模块的分页查询发生异常,异常信息为:{}", e.getMessage());
            return R.fail(e.getMessage());
        } catch (Exception e) {
            log.error("Article模块的分页查询发生异常,异常信息为:{}", e.getMessage());
            return R.fail("系统异常,请重试");
        }
    }

    /**
     * 首页归档统计
     *
     * @return
     */
    @GetMapping("/archivesTime")
    public R<List<ArchivesVo>> getArchivesTime() {
        try {
            return R.ok("查询成功!", articleService.getArchivesTime());
        } catch (GlobalException e) {
            log.error("Article模块的归档日期查询发生异常,异常信息为:{}", e.getMessage());
            return R.fail(e.getMessage());
        } catch (Exception e) {
            log.error("Article模块的归档日期查询发生异常,异常信息为:{}", e.getMessage());
            return R.fail("系统异常,请重试");
        }
    }

    /**
     * 首页友情链接
     *
     * @return
     */
    @GetMapping("/friendAddress")
    public R<List<FriendAddress>> getFriendAddress() {
        try {
            return R.ok("查询成功!", friendAddressService.getFriendAddress());
        } catch (GlobalException e) {
            log.error("Article模块的归档日期查询发生异常,异常信息为:{}", e.getMessage());
            return R.fail(e.getMessage());
        } catch (Exception e) {
            log.error("Article模块的归档日期查询发生异常,异常信息为:{}", e.getMessage());
            return R.fail("系统异常,请重试");
        }
    }

    /**
     * 首页参数信息接口
     *
     * @return
     */
    @GetMapping("/param")
    public R<Map<String, String>> getParamMap() {
        try {
            return R.ok("查询成功!", paramService.getParamMap());
        } catch (GlobalException e) {
            log.error("Article模块的归档日期查询发生异常,异常信息为:{}", e.getMessage());
            return R.fail(e.getMessage());
        } catch (Exception e) {
            log.error("Article模块的归档日期查询发生异常,异常信息为:{}", e.getMessage());
            return R.fail("系统异常,请重试");
        }
    }

    /**
     * 文章详情页面展示
     *
     * @return
     */
    @GetMapping("/articleById/{articleId}")
    public R<Article> getArticleById(@PathVariable("articleId") Long articleId) {
        try {
            return R.ok("查询成功!", articleService.selectById(articleId));
        } catch (GlobalException e) {
            log.error("Article模块的归档日期查询发生异常,异常信息为:{}", e.getMessage());
            return R.fail(e.getMessage());
        } catch (Exception e) {
            log.error("Article模块的归档日期查询发生异常,异常信息为:{}", e.getMessage());
            return R.fail("系统异常,请重试");
        }
    }

    @GetMapping("/articleByTag/{articleId}")
    public R<List<Article>> getArticleByTag(@PathVariable("articleId") Long articleId) {
        try {
            return R.ok("查询成功!", articleService.getArticleByTag(articleId));
        } catch (GlobalException e) {
            log.error("Article模块的归档日期查询发生异常,异常信息为:{}", e.getMessage());
            return R.fail(e.getMessage());
        } catch (Exception e) {
            log.error("Article模块的归档日期查询发生异常,异常信息为:{}", e.getMessage());
            return R.fail("系统异常,请重试");
        }
    }

    @GetMapping("/articleByArticleTypeId/{articleTypeId}")
    public R<List<Article>> getArticleByArticleTypeId(@PathVariable("articleTypeId") Long articleTypeId) {
        try {
            return R.ok("查询成功!", articleService.getArticleByArticleTypeId(articleTypeId));
        } catch (GlobalException e) {
            log.error("Article模块的归档日期查询发生异常,异常信息为:{}", e.getMessage());
            return R.fail(e.getMessage());
        } catch (Exception e) {
            log.error("Article模块的归档日期查询发生异常,异常信息为:{}", e.getMessage());
            return R.fail("系统异常,请重试");
        }
    }

    /**
     * 归档页面实现
     *
     * @param time
     * @return
     */
    @GetMapping("/archivesList/{time}")
    public R<List<ArticleVo>> getArticleById(@PathVariable("time") String time) {
        try {
            return R.ok("查询成功!", articleService.getArchivesList(time));
        } catch (GlobalException e) {
            log.error("Article模块的归档日期查询发生异常,异常信息为:{}", e.getMessage());
            return R.fail(e.getMessage());
        } catch (Exception e) {
            log.error("Article模块的归档日期查询发生异常,异常信息为:{}", e.getMessage());
            return R.fail("系统异常,请重试");
        }
    }

    @GetMapping("/getArticleList/{time}")
    public List<ArticleDto> getArticleList(@PathVariable("time") String time) {
        return articleService.getArticleList(time);
    }


    @PostMapping("/like")
    public R<Void> likeArticle(@RequestParam("articleId") Long articleId, @RequestParam("userId") Long userId) {
        try {
            String key = "article_like:" + articleId;
            // 判断用户是否已经点赞
            if (redisTemplate.opsForSet().isMember(key, String.valueOf(userId))) {
                return R.fail("您已经点过赞了");
            } else {
                // 如果用户未点赞，则将点赞记录存入 Redis
                redisTemplate.opsForSet().add(key, String.valueOf(userId));
                // 更新点赞数
                articleService.incrementLikeCount(articleId);
                return R.ok("点赞成功");
            }
        } catch (Exception e) {
            log.error("点赞功能发生异常，异常信息为:{}", e.getMessage());
            return R.fail("系统异常，请重试");
        }
    }

}
